package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.BOM;
import com.kgc.basic.pojo.Good;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.BOMMapper;
import com.kgc.basicprovider.service.BOMService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BOMServiceImpl implements BOMService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BOMMapper bomMapper;

    @Override
    public ResponseMsg addBom(BOM bom) {
        logger.info("BOMServiceImpl addBom bom:{}", bom);
        ResponseMsg responseMsg = new ResponseMsg();
        bom.setCreateDate(new Date());
        bom.setUpdateDate(new Date());
//        bom.setCreateUser("1111");
//        bom.setUpdateUser("1111");
        int count = bomMapper.addBOM(bom);
        logger.debug("bomMapper addBOM count:{}", count+",bom"+bom);
        if (count == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("bid",bom.getId());
        map.put("status",1);
        for (Good good:bom.getMaterial()){
            map.put("gid",good.getId());
            int num = bomMapper.addDetail(map);
            logger.debug("bomMapper material addDetail num:{}", num+",map"+map);
             if (num == 0) {
                 responseMsg.setCode("201");
                 return responseMsg;
             }
        }
        map.put("status",2);
        map.put("gid",bom.getProduct().getId());
        int num = bomMapper.addDetail(map);
        logger.debug("bomMapper product addDetail num:{}", num+",map"+map);
        if (num == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        responseMsg.setCode("200");
        return responseMsg;
    }

    @Override
    public ResponseMsg updateBom(BOM bom) {
        logger.info("BOMServiceImpl updateBom bom:{}", bom);
        ResponseMsg responseMsg = new ResponseMsg();
        bom.setUpdateDate(new Date());
//        bom.setUpdateUser("1111");
        int count = bomMapper.updateBOM(bom);
        logger.debug("BOMServiceImpl updateBom count:{}", count+",bom"+bom);
        if (count == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        int num = bomMapper.delDetail(bom.getId()+"");
        logger.debug("bomMapper delBOM num:{}", num+",id:"+bom.getId());
        if (num == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("bid",bom.getId());
        map.put("status",1);
        for (Good good:bom.getMaterial()){
            map.put("gid",good.getId());
            int nu = bomMapper.addDetail(map);
            logger.debug("bomMapper material addDetail nu:{}", nu+",map"+map);
            if (nu == 0) {
                responseMsg.setCode("201");
                return responseMsg;
            }
        }
        map.put("status",2);
        map.put("gid",bom.getProduct().getId());
        int nu = bomMapper.addDetail(map);
        logger.debug("bomMapper product addDetail nu:{}", nu+",map"+map);
        if (nu == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        responseMsg.setCode("200");
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteBom(String id) {
        logger.info("BOMServiceImpl deleteBom id:{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        int count = bomMapper.delBOM(id);
        logger.debug("bomMapper delBOM count:{}", count+",id"+id);
        if (count == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        count = bomMapper.delDetail(id);
        logger.debug("bomMapper delDetail count:{}", count+",id"+id);
        if (count == 0) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        responseMsg.setCode("200");
        return responseMsg;
    }

    @Override
    public ResponseMsg getBomById(String id) {
        logger.info("BOMServiceImpl getBomById id:{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        BOM bom = bomMapper.getBOMById(id);
        logger.debug("BOMServiceImpl getBomById id:{}", id+",bom"+bom);
        if (bom != null) {
            responseMsg.setCode("200");
            responseMsg.setData(bom);
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getBOMPage(Map<String, Object> map) {
        logger.info("BOMServiceImpl getBOMPage map:{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        PageHelper.startPage(Integer.parseInt(map.get("pageNow").toString()),Integer.parseInt(map.get("pageSize").toString()));
        List<BOM> list = bomMapper.getAllBOM(map);
        logger.debug("bomMapper getAllBOM list:{}", list+",map"+map);
        PageInfo<BOM> pageInfo = new PageInfo<>();
        if(list!=null&& !list.isEmpty()){
            pageInfo.setList(list);
            pageInfo.setPageNo(Integer.parseInt(map.get("pageNow").toString()));
            pageInfo.setPageSize(Integer.parseInt(map.get("pageSize").toString()));
            pageInfo.setTotal(bomMapper.getBOMCount(map));
        }
        if(pageInfo.getList()!=null&&pageInfo.getList().size()>0){
            responseMsg.setCode("200");
            responseMsg.setData(pageInfo);
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getAllBom() {
        logger.info("BOMServiceImpl getAllBom start...");
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> map = new HashMap<>();
        List<BOM> list = bomMapper.getAllBOM(map);
        logger.debug("bomMapper getAllBOM list:{}", list+",map"+map);
        if(list!=null&& !list.isEmpty()){
            responseMsg.setCode("200");
            responseMsg.setData(list);
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }
}
